﻿//============================================================
//author:zhxitao
//============================================================

using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using Saker.Kernel.Model;

namespace Saker.Kernel.DAL
{
    #region SysRole数据访问层
	/// <summary>
	///SysRole数据访问层
	/// </summary>	
	public partial class SysRoleDAL
	{
        #region SysRole数据访问层 自动生成
		/// <summary>
        /// 添加SysRole
        /// </summary>
        /// <param name="SysRole">SysRole</param>
        /// <returns>添加后的SysRole</returns>
        public SysRole Add
			(SysRole sysRole)
		{
				string sql ="INSERT INTO SysRoles (RoleGroupID, RoleName, RoleDescription)  VALUES (@RoleGroupID, @RoleName, @RoleDescription) select @@identity";
				SqlParameter[] para = new SqlParameter[]
					{
						new SqlParameter("@RoleGroupID", ToDBValue(sysRole.RoleGroupID)),
						new SqlParameter("@RoleName", ToDBValue(sysRole.RoleName)),
						new SqlParameter("@RoleDescription", ToDBValue(sysRole.RoleDescription)),
					};
					
				int newId = Convert.ToInt32(SqlHelper.ExecuteScalar(sql, para));
				return GetByRoleID(newId);
		}

		/// <summary>
        /// 根据主键删除SysRole
        /// </summary>
        /// <param name="RoleID">RoleID</param>
        /// <returns>影响的行数</returns>
        public int DeleteByRoleID(int roleID)
		{
            string sql = "DELETE SysRoles WHERE RoleID = @RoleID";

           SqlParameter[] para = new SqlParameter[]
			{
				new SqlParameter("@RoleID", roleID)
			};
		
            return SqlHelper.ExecuteNonQuery(sql, para);
		}
		
				
		/// <summary>
		/// 更新SysRole
		/// </summary>
		/// <param name="SysRole">SysRole</param>
		/// <returns></returns>
        public int Update(SysRole sysRole)
        {
            string sql =
                "UPDATE SysRoles " +
                "SET " +
			" RoleGroupID = @RoleGroupID" 
                +", RoleName = @RoleName" 
                +", RoleDescription = @RoleDescription" 
               
            +" WHERE RoleID = @RoleID";


			SqlParameter[] para = new SqlParameter[]
			{
				new SqlParameter("@RoleID", sysRole.RoleID)
					,new SqlParameter("@RoleGroupID", ToDBValue(sysRole.RoleGroupID))
					,new SqlParameter("@RoleName", ToDBValue(sysRole.RoleName))
					,new SqlParameter("@RoleDescription", ToDBValue(sysRole.RoleDescription))
			};

			return SqlHelper.ExecuteNonQuery(sql, para);
        }		
		
		/// <summary>
		/// 根据主键查询SysRole实体
		/// </summary>
		/// <param name="SysRole">SysRole</param>
		/// <returns>SysRole</returns>
        public SysRole GetByRoleID(int roleID)
        {
            string sql = "SELECT * FROM SysRoles WHERE RoleID = @RoleID";
            using(SqlDataReader reader = SqlHelper.ExecuteDataReader(sql, new SqlParameter("@RoleID", roleID)))
			{
				if (reader.Read())
				{
					return ToModel(reader);
				}
				else
				{
					return null;
				}
       		}
        }
		/// <summary>
		/// SqlDataReader对象转换为SysRole实体
		/// </summary>
		/// <param name="reader">SqlDataReader对象</param>
		/// <returns>SysRole实体</returns>
		public SysRole ToModel(SqlDataReader reader)
		{
			SysRole sysRole = new SysRole();

			sysRole.RoleID = (int)ToModelValue(reader,"RoleID");
			sysRole.RoleGroupID = (int)ToModelValue(reader,"RoleGroupID");
			sysRole.RoleName = (string)ToModelValue(reader,"RoleName");
			sysRole.RoleDescription = (string)ToModelValue(reader,"RoleDescription");
			return sysRole;
		}
		/// <summary>
		/// 得到数据总行数
		/// </summary>
		/// <returns></returns>
		public int GetTotalCount()
		{
			string sql = "SELECT count(1) FROM SysRoles";
			return (int)SqlHelper.ExecuteScalar(sql);
		}
		/// <summary>
		/// 分页，此方法仅在sql 2005下使用
		/// </summary>
		/// <param name="minrownum">起始行数</param>
		/// <param name="maxrownum">>结束行数</param>
		/// <returns>SysRole集合</returns>
		public IEnumerable<SysRole> GetPagedData(int minrownum,int maxrownum)
		{
			string sql = "SELECT * from(SELECT *,row_number() over(order by RoleID) rownum FROM SysRoles) t where rownum>=@minrownum and rownum<=@maxrownum";
			using(SqlDataReader reader = SqlHelper.ExecuteDataReader(sql,
				new SqlParameter("@minrownum",minrownum),
				new SqlParameter("@maxrownum",maxrownum)))
			{
				return ToModels(reader);					
			}
		}
        /// <summary>
        /// 分页，调用存储过程
        /// </summary>
        /// <param name="pageIndex">页码</param>
        /// <param name="recordCount">记录数</param>
        /// <param name="pageSize">每页记录数</param>
        /// <param name="pageCount">总页数</param>
        /// <param name="whereCondition">where</param>
        /// <returns>DataTable数据集</returns>
        public DataTable GetPagingRecord(int pageIndex, ref int recordCount, int pageSize, ref int pageCount,string whereCondition)
        {
            return SqlHelper.GetPagingRecord(pageIndex,ref recordCount,pageSize,ref pageCount,"SysRoles","RoleID",whereCondition,"*","RoleID",true);
        }
		/// <summary>
		/// 得到所有SysRole集合
		/// </summary>
		/// <returns>所有集合</returns>
		public IEnumerable<SysRole> GetAll()
		{
			string sql = "SELECT * FROM SysRoles";
			using(SqlDataReader reader = SqlHelper.ExecuteDataReader(sql))
			{
				return ToModels(reader);			
			}
		}
		/// <summary>
		/// SqlDataReader对象转换到SysRole集合
		/// </summary>
		/// <param name="reader">SqlDataReader对象</param>
		/// <returns>SysRole集合</returns>
		protected IEnumerable<SysRole> ToModels(SqlDataReader reader)
		{
			var list = new List<SysRole>();
			while(reader.Read())
			{
				list.Add(ToModel(reader));
			}	
			return list;
		}		
		/// <summary>
		/// 转换数据值
		/// </summary>
		/// <param name="value">object类型</param>
		/// <returns>object对象</returns>
		protected object ToDBValue(object value)
		{
			if(value==null)
			{
				return DBNull.Value;
			}
			else
			{
				return value;
			}
		}
		/// <summary>
		/// 转换实体值
		/// </summary>
		/// <param name="reader">SqlDataReader对象</param>
		/// <param name="columnName">列名称</param>
		/// <returns>值</returns>
		protected object ToModelValue(SqlDataReader reader,string columnName)
		{
			if(reader.IsDBNull(reader.GetOrdinal(columnName)))
			{
				return null;
			}
			else
			{
				return reader[columnName];
			}
		}
        #endregion
	}
    #endregion
}
